package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC167 {
    public long startExecuteTime = System.currentTimeMillis();

    class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int[] res = new int[2];
            for (int i = 0; i < numbers.length - 1; i++) {
                int targetIndex = findByBinarySearch(numbers, i + 1, numbers.length - 1, target - numbers[i]);
                if (targetIndex != -1) {
                    res[0] = i + 1;
                    res[1] = targetIndex + 1;
                    return res;
                }
            }

            return res;
        }

        public int findByBinarySearch(int[] arr, int start, int end, int target) {
            int mid;
            while (start <= end) {
                mid = start + (end - start) / 2;
                if (arr[mid] == target) return mid;
                if (arr[mid] > target) end = mid - 1;
                else start = mid + 1;
            }

            return -1;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[2,7,11,15]");
        System.out.println(solution.twoSum(arr, 9));
    }

    public static void main(String[] args) throws Exception {
        LC167 an = new LC167();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
